package com.awesome.databindingdemo.databinding2;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.awesome.databindingdemo.R;
import com.awesome.databindingdemo.databinding.ActivityMainBinding;
import com.awesome.databindingdemo.databinding2.adapter.SwordsmanAdapter;
import com.awesome.databindingdemo.databinding2.bean.ObFSwordsman;
import com.awesome.databindingdemo.databinding2.bean.ObSwordsman;
import com.awesome.databindingdemo.databinding2.bean.Swordsman;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        // 设置引用类型的数据
        Swordsman swordsman = new Swordsman("张无忌", "S");
        binding.setSwordsman(swordsman);
        // 设置别名的类（TODO）
//        binding.setMan(new Swordsman("张无忌","S"));
        // 第一种获取id方式
//        binding.btnNext.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ShowLogUtil.info("点击");
//            }
//        });
        // 第二种绑定方式
        binding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 无法修改值
                swordsman.setName("成昆");
            }
        });
        // 设置基本类型的数据
        binding.setName("风清扬");
        binding.setAge(30);
        binding.setSex(true);

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("1");
        arrayList.add("2");
        binding.setList(arrayList);

        Map<String, String> map = new HashMap<>();
        map.put("age", "30");
        binding.setMap(map);

        String[] arrays = {"张无忌", "慕容龙城"};
        binding.setArrays(arrays);

        binding.setTime(new Date());

        // 监听实体类@Bindable、notifyPropertyChanged(BR.name)
        ObSwordsman obSwordsman = new ObSwordsman("任我行", "A");
        binding.setObSwordsman(obSwordsman);
        binding.btnObSwordsman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                obSwordsman.setName("石破天");
                obSwordsman.setLevel("SS");
            }
        });

        // 监听实体类的某个属性ObservableField
        ObFSwordsman obFSwordsman = new ObFSwordsman("周芷若", "S");
        binding.setObFSwordsman(obFSwordsman);
        binding.btnObFSwordsman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                obFSwordsman.name.set("赵敏");
                obFSwordsman.setLevel("A");
            }
        });

        // 监听集合数据
        ObservableArrayList<Swordsman> list = new ObservableArrayList<>();
        Swordsman swordsman1 = new Swordsman("金毛狮王", "SS");
        Swordsman swordsman2 = new Swordsman("谢逊", "SS");
        list.add(swordsman1);
        list.add(swordsman2);
        binding.setObservableArrayList(list);
        binding.btnObservableArrayList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                swordsman1.setName("小龙女");
                swordsman1.setLevel("S");
                swordsman2.setName("杨过");
                swordsman2.setLevel("S");
                list.add(swordsman1);
            }
        });

        ObSwordsman obSwordsman1 = new ObSwordsman("任我行", "A");
        binding.setObSwordsmanEt(obSwordsman1);
        binding.btnObSwordsmanEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                obSwordsman1.setName(binding.et.getText().toString().trim());
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        binding.recycler.setLayoutManager(linearLayoutManager);
        List<Swordsman> list1 = new ArrayList<>();
        Swordsman swordsman3 = new Swordsman("杨影枫", "SS");
        Swordsman swordsman4 = new Swordsman("月眉儿", "A");
        list1.add(swordsman3);
        list1.add(swordsman4);
        SwordsmanAdapter adapter = new SwordsmanAdapter(list1);
        binding.recycler.setAdapter(adapter);

        IpInfoFragment fragment = new IpInfoFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.ll_container, fragment);
        ft.commit();
    }
}